package gui;

import javax.swing.*;

import multiformat.BinaryBase;
import multiformat.DecimalBase;
import multiformat.FixedPointFormat;
import multiformat.FloatingPointFormat;
import multiformat.HexBase;
import multiformat.OctalBase;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SelectionView extends JPanel implements ActionListener{
	private JRadioButton decimal = new JRadioButton();	
	private JRadioButton binary = new JRadioButton();
	private JRadioButton octal = new JRadioButton();
	private JRadioButton hexa = new JRadioButton();
	private JRadioButton fixed = new JRadioButton();
	private JRadioButton floating = new JRadioButton();	
	CalculatorModel c;	
	
	public SelectionView(){		
		
		//Setup the Layout
		this.setLayout(new GridLayout(6, 1));
		
		//Add labels to radio buttons
		decimal.setLabel("Dec");
		binary.setLabel("Bin");
		octal.setLabel("Oct");
		hexa.setLabel("Hex");	
		
		//Set the selected radio button
		decimal.setSelected(true);
		
		//Create a new group for the radio buttons of the bases
		ButtonGroup bases = new ButtonGroup();
		bases.add(decimal);
		bases.add(binary);
		bases.add(octal);
		bases.add(hexa);		

		//Add radiobuttons
		this.add(decimal);		
		this.add(binary);		
		this.add(octal);	
		this.add(hexa);		
		
		//Set actionCommands
		decimal.setActionCommand("decimalRadio");
		binary.setActionCommand("binaryRadio");
		octal.setActionCommand("octalRadio");
		hexa.setActionCommand("hexaRadio");
		
		
		//Add actionlisteners
		decimal.addActionListener(this);
		binary.addActionListener(this);
		octal.addActionListener(this);
		hexa.addActionListener(this);
		
		//Add labels to radio buttons
		fixed.setLabel("Fixed");
		floating.setLabel("Float");	
		
		//Set selected radio button
		fixed.setSelected(true);
		
		//Create new group for the radio buttons of the formats
		ButtonGroup formats = new ButtonGroup();
		formats.add(fixed);
		formats.add(floating);
		
		//Add radiobuttons
		this.add(fixed);		
		this.add(floating);
		
		//Set ActionCommands
		fixed.setActionCommand("fixedRadio");
		floating.setActionCommand("floatingRadio");
		
		//Add actionlisteners
		fixed.addActionListener(this);
		floating.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e){
		c = (CalculatorModel) e.getSource();		
		
		if(e.getActionCommand() == "decimalRadio"){
			c.setBase(new DecimalBase());
		}
		if(e.getActionCommand() == "binaryRadio"){
			c.setBase(new BinaryBase());
		}
		if(e.getActionCommand() == "octalRadio"){
			c.setBase(new OctalBase());
		}
		if(e.getActionCommand() == "hexaRadio"){
			c.setBase(new HexBase());
		}
		if(e.getActionCommand() == "fixedRadio"){
			c.setFormat(new FixedPointFormat());
		}
		if(e.getActionCommand() == "floatingRadio"){
			c.setFormat(new FloatingPointFormat());	
		}
	}
}